package com.kibey.tools;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.nio.charset.Charset.defaultCharset;

/**
 * Created by mchwind on 18/9/11.
 * 翻译给的多语言是一个表格，转成安卓的资源格式
 */
public class Excel2AndroidStrings {
    // 翻译给到的文件，里面包含了多国语言
    static File originFile = Utils.getResFile("translate.txt");

    private static ArrayList match;
    private static Map<String, String> mDeMap;

    public static void main(String[] args) throws IOException {
        List<String> excelStrings = FileUtils.readLines(originFile, defaultCharset());
        Map<String, String[]> excelStringArrayList = new HashMap<>();
        MergeStringsFromApp.logd(excelStrings.size());
        for (String string : excelStrings) {
            String[] strs = string.split("\t");
            excelStringArrayList.put(strs[0], strs);
        }

        List<AppStringEntry> appStringEntries = new ArrayList<>();
        List<String> appStringsList = FileUtils.readLines(MergeStringsFromApp.getStringsFile());
        for (String s : appStringsList) {
            String name = Utils.getStringsName(s);
            String value = Utils.getStringsValue(s);
            if (null != name) {
                appStringEntries.add(new AppStringEntry(name, value));
            }

        }


        ArrayList<StringsData> stringsDatas = new ArrayList<>();
        match = new ArrayList<>();

        StringsData data = new StringsData();
        stringsDatas.add(data);
        data.key = "";
        data.values = excelStringArrayList.get("strings_zh");

        for (AppStringEntry appStringEntry : appStringEntries) {
            String appStringEntryValue = appStringEntry.value;
//            if (appStringEntry.value.contains("\\n")) {
//                appStringEntryValue = appStringEntry.value.replaceAll("\\\\n", "");
//            }
            if (excelStringArrayList.containsKey(appStringEntryValue)) {
                data = new StringsData();
                String value = appStringEntry.value;
                String key = appStringEntry.res_id;
                data.key = key;
                data.values = excelStringArrayList.get(value);
                stringsDatas.add(data);
                match.add(key);
            } else {
                if (!inOldRes(appStringEntry.res_id)) {
                    System.out.println(Utils.getAndroidStringItem(appStringEntry.res_id, appStringEntry.value));
                }
            }
        }

        exportFile(stringsDatas);

        logd("导出完成");

        FindUnTranslateStrings.find("app");
    }

    private static boolean inOldRes(String res_id) {
        if (mDeMap == null) {
            mDeMap = Utils.getStrings4Map(Utils.getOldStringFile("de"));
        }
        return mDeMap.containsKey(res_id);
    }

    private static void exportFile(List<StringsData> appStringEntries) throws IOException {
        List<String>[] result = new ArrayList[appStringEntries.get(0).values.length];

        for (StringsData item : appStringEntries) {
            if (item.key != "") {
                try {
                    for (int i = 0; i < item.values.length; i++) {
                        if (item.values[i] == null || "null".equals(item.values[i])) {
                            continue;
                        }
                        String stringItem = Utils.getAndroidStringItem(item.key, item.values[i]);
                        try {
                            if (result[i] == null) {
                                result[i] = new ArrayList<>();
                            }
                            result[i].add(stringItem);
                        } catch (Exception e) {
                            System.err.println(" size " + item.values.length + " " + stringItem);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        for (int i = 0; i < result.length; i++) {
            File file = Utils.getResFile("strings", appStringEntries.get(0).values[i] + ".xml");
            file.delete();
            file.createNewFile();
            // write
            FileUtils.write(file, "<resources>\n", defaultCharset(), true);

            // 旧的资源
            File oldFile = Utils.getResFile("strings_old", appStringEntries.get(0).values[i] + ".xml");
            List<Utils.StringsEntry> oldStringsList = null;
            if (oldFile.exists()) {
                oldStringsList = Utils.strings2Entry(oldFile);
                for (Utils.StringsEntry entry : oldStringsList) {
                    FileUtils.write(file, Utils.getAndroidStringItem(entry), defaultCharset(), true);
                }
            }

            // 新的资源
            for (String s : result[i]) {
                FileUtils.write(file, s, defaultCharset(), true);
            }
            FileUtils.write(file, "</resources>\n", defaultCharset(), true);

        }
    }

    static class StringsData {
        String key;
        String[] values;
    }

    static class AppStringEntry {
        public AppStringEntry(String res_id, String value) {
            this.res_id = res_id;
            this.value = value;
        }

        String res_id;
        String value;
    }

    public static void logd(Object file) {
        System.out.println(file);
    }
}
